#include <stdlib.h>
#include "inputComand.h"
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

int voiceInit(struct InputCommander *voicer, char *ipAdress, char *port)
{
  int fd;

  if ((fd = serialOpen(voicer->devicesName, 115200)) < 0)
  {
    printf("Unable to open serial device: %s\n", strerror(errno));
    exit(-1);
  }
  voicer->fd = fd;
};

int voiceGetCommand(struct InputCommander *voicer)
{

  int nread = 0;
  memset(voicer->command, '\0', sizeof(voicer->command));
  nread = read(voicer->fd, voicer->command, sizeof(voicer->command));
  if (nread == 0)
  {
    printf("uart for voice read is over time\n");
    return 0;
  }
  else
  {
    return nread;
  }
};
struct InputCommander voiceControl = {
    .commandName = "voice",
    .devicesName = "/dev/ttyS5",
    .Init = voiceInit,
    .command = {'\0'},
    .getCommand = voiceGetCommand,
    .log = {'\0'},
    .next = NULL

};

struct InputCommander *addVoiceControlCommandLink(struct InputCommander *pthead)
{
  if (pthead == NULL)
  {
    return &voiceControl;
  }
  else
  {

    voiceControl.next = pthead;
    pthead = &voiceControl;
    return pthead;
  }
}
